from flask import jsonify
from API import OperationMysql, request, app
import time
import json
# 自定义的数据库类
op_mysql = OperationMysql()

# 查询所有备忘录
# post
@app.route('/memo/index', methods=['post'])
def get_memo():
    if request.method == 'POST':
        res = op_mysql.search_one("SELECT * FROM memo order by id desc")
        return jsonify(res)

# 添加备忘录
# post
@app.route('/memo/add', methods=['post'])
def add_memo():
    if request.method == 'POST':
        formData = 'multipart/form-data'
        isFormData = formData in request.headers['Content-Type']
        content = ''
        memoTime = ''
        level = ''
        if isFormData:
            content = request.form['content']
            memoTime = request.form['time']
            level = request.form['level']
        else:
            content = json.loads(request.data.decode("utf-8")).get("content")
            memoTime = json.loads(request.data.decode("utf-8")).get("time")
            level = json.loads(request.data.decode("utf-8")).get("level")
        t = time.time()
        t = int(t)
        res = op_mysql.insert_one("INSERT INTO memo (content, creation_time, state, user_id, id, update_time, time, level) VALUES ('%s','%s',1,1,0,'%s','%s','%s')" % (content, t, t, memoTime, level))
        return jsonify(res)

# # GET
# @app.route('/user', methods=['GET'])
# def get_user():
#     return jsonify({'user1': 1})

def Memo():
    print("\033[1;32m执行备忘录APi\033[0m")